//
//  SelectedLoanViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/15.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import SwiftyJSON

class SelectedLoanViewController: KMViewController {
    
    fileprivate let cellId = "selectedLoanCell"
    fileprivate var dataArray = [GenericLoansModel]()
    
    fileprivate var page = 1
    fileprivate var loanType = "" //贷款类型
    
    fileprivate var tableView : UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = loanSelection
    
        createTableView()

    }
    
    //MARK: 创建tableView
    fileprivate func createTableView() {
        
        let titleLabel = UILabel.init(frame: .init(x: 0, y: 0, width: ScreenW, height: RATIO_H(maxNum: 40)))
        titleLabel.text = dayToAccount
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        titleLabel.addSubview(lineView)
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(NewCityLoanCell.self, forCellReuseIdentifier: cellId)
        tableView.separatorStyle = .none
        tableView.tableHeaderView = titleLabel
        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        KMTool.setRefresh(with: tableView, headerAction: {[weak self] in
            
            self?.page = 1
            self?.getData()
            
        }) {[weak self] in
            
            self?.getData()
        }
        
        self.tableView.mj_header.beginRefreshing()
    }
    
    //MARK: 获取数据
    fileprivate func getData() {
        
        KMTool.requestGetData(URLString: url_SelectedLoan, parameters: ["psw":"kameng98_loan","p":"\(self.page)"], success: { (data) in
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            let json = JSON.init(data)
            if let array = json.arrayObject {
                
                if self.page == 1 {
                    self.dataArray.removeAll()
                }
                
                if array.count > 0 {
                    self.page += 1
                }
                
                for item in array {
                    let model = GenericLoansModel.setModel(with: item as! [String : Any])
                    self.dataArray.append(model)
                }
                
                self.tableView.reloadData()
            }
            
            
            
        }) {
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    

}

extension SelectedLoanViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 140)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? NewCityLoanCell
        cell?.model = dataArray[indexPath.row]
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let loanDetailsVC = LoanDetailsViewController.init(model: dataArray[indexPath.row])
        loanDetailsVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(loanDetailsVC, animated: true)
        
    }
}
